#ifndef __KERNEL_TIME_H__
#define __KERNEL_TIME_H__

#include "std/list.h"
#include "arch/cpu.h"

#ifdef __cplusplus
extern "C" {
#endif

struct time_struct
{
	unsigned char  second;
	unsigned char  minute;
	unsigned char  hour;
	unsigned char  week;
	unsigned char  day;
	unsigned char  month;
	unsigned short year;
} PACKED;

typedef struct time_struct times_t;

#define TIME_MAX_SEC			(60)
#define TIME_MAX_MIN			(60)
#define TIME_MAX_HOUR			(24)
#define TIME_MAX_MON			(12)

#define IS_LEAP_YEAR(y)			(((((y) % 100) != 0) && (((y) % 4) == 0)) || (((y) % 400) == 0))

#define MON_JAN					(1)
#define MON_FEB					(2)
#define MON_MAR					(3)
#define MON_APR					(4)
#define MON_MAY					(5)
#define MON_JUN					(6)
#define MON_JUL					(7)
#define MON_AUG					(8)
#define MON_SEP					(9)
#define MON_OCT					(10)
#define MON_NOV					(11)
#define MON_DEC					(12)

// global time structure
extern times_t global_time;

// global variable
extern unsigned int volatile jiffies;

// this is for kernel timer
typedef struct timer_list
{
	unsigned int index;
	listnode_t *timer_list;
}
timer_list_t;

typedef struct timer
{
	listnode_t list;
	unsigned int expire;
	unsigned int input;
	void (*on_timer)(struct timer*, uint);
}
timer_t;

// init a timer object
#define init_timer(tmr, timelen, _on_timer, _input)	\
do { \
	extern unsigned int volatile jiffies; \
	kassert(NULL != (tmr)); \
	(tmr)->expire = jiffies + (timelen); \
	(tmr)->input = (uint)(_input); \
	(tmr)->on_timer = (_on_timer); \
	listnode_init((tmr)->list); \
} while (0)

// add a timer
bool add_timer(timer_t *timer);

// remove a timer
void remove_timer(timer_t *timer);

// enable task scheduler
void sched_enable(void);

// global initialization
void global_init_time_module(void);

#ifdef __cplusplus
}
#endif
#endif
/* EOF */
